﻿var grid = null;
var PageSize = 40;
var url = "MyMessage.aspx?";

$(document).ready(function () {
    grid = $("#maingrid").ligerGrid({
        columns: [
        {
            display: '', name: '已读', width: 30, align: 'center', type: 'checkbox', render: function (rowdata, index, value) {
                return "<img id='img_" + rowdata.RID + "' style='margin-top: " + (rowdata.RDate > '' ? '2px' : '5px') + ";'src='../Images/" + (rowdata.RDate > '' ? 'read.png' : 'Mail.jpg') + "' />";
            }
        },
        { display: '标题', name: 'Title', width: 600, align: 'left' },
        { display: '时间', name: 'CDate', width: 150, align: 'left', type: 'date' }
        ],
        pageSize: PageSize,
        toolbar: {},
        url: url + "Action=GetList",
        width: '100%',
        height: '99%',
        dataAction: 'local',
        pageSizeOptions: [3, 20, 25, 30, 35, 40],
        checkbox: true,
        delayLoad: true,
        rownumbers: true,
        frozen: false,
        sortName: 'rID',
        sortOrder: 'ASC',
        detail: { onShowDetail: f_showDetail, height: 'auto' },
        onDblClickRow: function (row, rowindex, rowDomElement) {
            $.ajax({
                type: 'post',
                url: url + 'Action=Process&Rids=' + row.RID,
                dataType: 'text',
                cache: false,
                success: function (msg) {
                    parent.RefreshMessage(); //刷新站内信息处理记录条数
                    switch (row.FunID.toString()) {
                        case "FA01": //Budget Management
                            ShowDetail("Budget.aspx");
                            break;
                        case "FA02": //Task Management
                            ShowDetail("Task.aspx");
                            break;
                        case "FA03": //Subtask Management
                            ShowDetail("Subtask.aspx");
                            break;
                        case "FA04": //Labor Hours Management
                            ShowDetail("LaborHours.aspx");
                            break;
                        case "FA05": //Additional HRS Request
                            ShowDetail("AdditionalHRS.aspx");
                            break;

                    }
                }
            });
        }
    });
    //加载搜索框
    InitForm();

    //加载工具条
    loadToolbar();
})


//显示明细
function f_showDetail(row, detailPanel, callback) {
    $("#img_" + row.RID).attr("src", "../Images/read.png");
    $("#img_" + row.RID).css("margin-top", "2px");
    var showgrid1 = document.createElement('div');
    $(detailPanel).append(showgrid1);
    $(showgrid1).css('margin-top', 5).css('margin-left', 20).html(row.Message.replace(/\n/g, "<br/>"));
    if (!row.Readed)//未读
    {
        ProcessData(row.RID, 0);
        parent.RefreshMessage(); //刷新站内信息处理记录条数
    }
}


function ShowDetail(_url) {
    //$("#frmApply").attr("src", _url);
    document.location = _url;
}

//搜索条件
function InitForm() {

    $("#drpStatus").ligerComboBox({
        lable: "状态", cancelable: false, data: [{ id: '0', text: '未读' }, { id: '1', text: '全部' }], textField: 'text', initValue: '0', width: 100, labelWidth: 50,
        onSelected: function (value, text) {
            grid.set('parms', {
                Status: value
            });
            grid.options.newPage = 1;
            grid.loadData();

        }
    });
}


//工具条
function loadToolbar() {
    var griditems = [];
    griditems[griditems.length] = { click: toolbarClick, text: "设为已读", img: "../images/Frozen.png", id: "Disable" };
    grid.toolbarManager.set('items', griditems);
}

//工具条事件
function toolbarClick(item) {
    switch (item.id) {
        case "Disable":
            var rids = GetClientRID();
            ProcessData(rids, 1);
            break;
    }
}


/**
* 改变状态
*/
function ProcessData(rids, type) {
    //var rids = GetClientRID();
    if (rids != "") {
        $.ajax({
            type: 'post',
            url: url + 'Action=Process&Rids=' + rids,
            dataType: 'text',
            cache: false,
            success: function (msg) {
                parent.RefreshMessage(); //刷新站内信息处理记录条数
                if (type == 1) {
                    if (msg == "") {
                        toastrSuccess("Processed successfully/处理成功!");
                        grid.options.newPage = 1;
                        grid.loadData();
                    } else {
                        toastrError("Processed Failed/处理失败!<br/>" + msg);
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                ShowError("ProcessData()", XMLHttpRequest, textStatus, errorThrown);
            }
        });
    }
}


//获取选中列表ID
function GetClientRID() {
    var rows = grid.getSelectedRows();
    if (!rows.length) {
        toastrWarning('Please select lines to be processed!<br/>请选择需要处理的行!');
        return "";
    }
    var ids = [];
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].RID);
    }
    return ids.join(',');
}

/**
* 显示错误
* @param {[type]} fun            [description]
* @param {[type]} XMLHttpRequest [description]
* @param {[type]} textStatus     [description]
* @param {[type]} errorThrown    [description]
*/
function ShowError(fun, XMLHttpRequest, textStatus, errorThrown) {
    var mess = "错误!请把次错误信息发送给管理员<br/>";
    mess += "function:" + fun + "<br/>";
    mess += "XMLHttpRequest.status:" + XMLHttpRequest.status + "<br/>";
    mess += "XMLHttpRequest.readyState:" + XMLHttpRequest.readyState + "<br/>";
    mess += "textStatus:" + textStatus;
    toastrError(mess);
}